package com.yxy.handler;

import com.yxy.common.Result;
import com.yxy.constant.ExceptionConstant;
import com.yxy.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.ReflectionException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.net.ConnectException;
import java.sql.SQLSyntaxErrorException;

/**
 * 全局异常拦截器
 */

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 捕获业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public ResponseEntity<Result<String>> exceptionHandler(BaseException ex){
        log.error("异常信息：{}", ex.getMessage());
        Result<String> error = Result.error(ex.getMessage());
        return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
    }

    /**
     * 捕获sql异常
     * @param ex
     */
    @ExceptionHandler
    public ResponseEntity<Result<String>> exceptionHandler(SQLSyntaxErrorException ex){
        String message = ex.getMessage();
        log.error("Sql异常信息：{}", message);
        return new ResponseEntity<>(Result.error(ExceptionConstant.DATABASE_EXCEPTION), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 捕获数据库异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public ResponseEntity<Result<String>> exceptionHandler(ConnectException ex){
        String message = ex.getMessage();
        log.error("数据库连接异常信息：{}", message);
        return new ResponseEntity<>(Result.error(ExceptionConstant.DATABASE_CONNECTION_ERROR), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 捕获反射异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public ResponseEntity<Result<String>> exceptionHandler(ReflectionException ex){
        String message = ex.getMessage();
        log.error("反射异常信息：{}", message);
        return new ResponseEntity<>(Result.error(ExceptionConstant.SERVER_ERROR), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 捕获参数异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public ResponseEntity<Result<String>> exceptionHandler(IllegalArgumentException ex){
        String message = ex.getMessage();
        log.error("参数异常信息：{}", message);
        return new ResponseEntity<>(Result.error(ExceptionConstant.PARAMETER_EXCEPTION), HttpStatus.BAD_REQUEST);
    }

    /**
     * 捕获空指针异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public ResponseEntity<Result<String>> exceptionHandler(NullPointerException ex){
        String message = ex.getMessage();
        log.error("空指针异常：{}", message);
        return new ResponseEntity<>(Result.error(ExceptionConstant.NULL_DATA_EXCEPTION), HttpStatus.BAD_REQUEST);
    }



    /**
     * 捕获请求异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public ResponseEntity<Result> exceptionHandler(HttpRequestMethodNotSupportedException ex){
        String message = ex.getMessage();
        log.error("请求异常：{}", message);
        return new ResponseEntity<>(Result.error(ExceptionConstant.REQUEST_EXCEPTION), HttpStatus.BAD_REQUEST);
    }
}
